<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$uniacid=$_SESSION['uniacid'];
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('未找到兑换券!','error');
	}
	$order=pdo_get('jy_sendgoods_order',array('id'=>$id));
	//检查兑换券是否有人领取
	if($order['isover']==1){
		exi('本次领取失败，该兑换券已被领完','error');
	}
	$log=pdo_get('jy_sendgoods_ex_log',array('order_id'=>$id,'smid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($log){
		exi('您已领取过,不可重复领取','error');
	}
	//新增领取记录
	$orderno=get_ordersn('jy_sendgoods_order','orderno');
	$data=array(
		'uniacid'=>1,
		'smid'=>$_SESSION['uid'],
		'order_id'=>$id,
		'orderno' =>'ex'.$orderno,
		'createtime'=>TIMESTAMP,
		);
	pdo_insert('jy_sendgoods_ex_log',$data);
	$update=array();
	if(($order['buynum']-$order['sendnum'])==1){
		$update['isover']=1;
	}
	$update['sendnum']=$order['sendnum']+1;
	pdo_update('jy_sendgoods_order',$update,array('id'=>$id));
	exi('领取成功','','jy_sendgoods/s_card/');
}
	

?>